#include "featureservice.h"
#include <qsqlquery.h>
#include <QDateTime>
#include "autoDatabase.h"

FeatureService::FeatureService()
{

}

bool FeatureService::createTable()
{
    // 获取数据连接
    AutoDatabase autoDb;

    // 数据库查询
    QSqlQuery query(autoDb.getDb());
    return query.exec(QString("CREATE TABLE IF NOT EXISTS t_feature("
                              "`id` varchar(32) NOT NULL COMMENT '特征编码',"
                              "`userId` varchar(32) NOT NULL COMMENT '用户编码',"
                              "`image` varchar(255) DEFAULT NULL COMMENT '人脸路径',"
                              "`md5` varchar(64) DEFAULT NULL COMMENT '人脸MD5值',"
                              "`data` varchar(2048) DEFAULT NULL COMMENT '人脸特征',"
                              "`createTime` datetime default now() COMMENT '提取时间',"
                              "`reserver1` varchar(128) default NULL COMMENT '保留字段',"
                              "`reserver2` varchar(128) default NULL COMMENT '保留字段',"
                              "primary key(`id`),"
                              "key `idx_feature_user`(`userId`, `md5`)"
                              ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='人脸特征';"));
}

bool FeatureService::add(FeatureInfo feature)
{
    // 获取数据连接
    AutoDatabase autoDb;

    // 数据库查询
    QString sql = "insert into t_feature(`id`, `userId`, `image`, `md5`, `data`,`createTime`)"
                  "values(:id, :userId, :image, :md5, :data, :createTime)";
    QSqlQuery query(autoDb.getDb());
    query.prepare(sql);
    query.bindValue(":id", feature.id);
    query.bindValue(":userId", feature.userId);
    query.bindValue(":image", feature.image);
    query.bindValue(":md5", feature.md5);
    query.bindValue(":data", feature.data);
    query.bindValue(":createTime", feature.createTime);
    return query.exec();
}

bool FeatureService::remove(QString id)
{
    // 获取数据连接
    AutoDatabase autoDb;

    // 移除数据记录
    QString sql = "delete from t_feature where `id`=:id";
    QSqlQuery query(autoDb.getDb());
    query.prepare(sql);
    query.bindValue(":id", id);
    return query.exec();
}

bool FeatureService::removeUser(QString userId)
{
    // 获取数据连接
    AutoDatabase autoDb;

    // 移除数据记录
    QString sql = "delete from t_feature where `userId`=:userId";
    QSqlQuery query(autoDb.getDb());
    query.prepare(sql);
    query.bindValue(":userId", userId);
    return query.exec();
}

bool FeatureService::update(FeatureInfo feature)
{
    // 获取数据连接
    AutoDatabase autoDb;

    // 更新数据记录
    QString sql = "update t_feature set `userId`=:userId, `image`=:image, `md5`=:md5,"
                  "`data`=:data, `createTime`=:createTime"
                  "where id=:id";
    QSqlQuery query(autoDb.getDb());
    query.prepare(sql);
    query.bindValue(":userId", feature.userId);
    query.bindValue(":image", feature.image);
    query.bindValue(":md5", feature.md5);
    query.bindValue(":data", feature.data);
    query.bindValue(":createTime", feature.createTime);
    query.bindValue(":id", feature.id);
    return query.exec();
}

bool FeatureService::find(QString userId, QVector<FeatureInfo>& features)
{
    // 获取数据连接
    AutoDatabase autoDb;

    // 查询数据记录
    QString sql = "select * from t_feature where `userId`=:userId";
    QSqlQuery query(autoDb.getDb());
    query.prepare(sql);
    query.bindValue(":userId", userId);

    // 打印查询结果
    if (query.exec()) {
        while (query.next()) {
            FeatureInfo feature;
            feature.id = query.value("id").toString();
            feature.userId = query.value("userId").toString();
            feature.image = query.value("image").toInt();
            feature.md5 = query.value("md5").toString();
            feature.image = query.value("image").toString();
            feature.data = query.value("data").toInt();
            feature.createTime = query.value("createTime").toDateTime().toString("yyyy-MM-dd hh:mm:ss");
            features.push_back(feature);
        }
        return true;
    }
    return false;
}

bool FeatureService::findAll(QVector<FeatureInfo>& features)
{
    // 获取数据连接
    AutoDatabase autoDb;

    // 查询数据记录
    QString sql = "select * from t_feature";
    QSqlQuery query(autoDb.getDb());
    query.prepare(sql);

    // 执行结果查询
    bool success = query.exec();
    if (success) {
        // 打印查询结果
        while (query.next()) {
            FeatureInfo feature;
            feature.id = query.value("id").toString();
            feature.userId = query.value("userId").toString();
            feature.image = query.value("image").toInt();
            feature.md5 = query.value("md5").toString();
            feature.image = query.value("image").toString();
            feature.data = query.value("data").toInt();
            feature.createTime = query.value("createTime").toDateTime().toString("yyyy-MM-dd hh:mm:ss");
            features.push_back(feature);
        }
    }
    return success;
}
